import sys
import typing
from PyQt5.QtCore import QThread, pyqtSignal, QThreadPool, QRunnable
from PyQt5.QtWidgets import (
    QApplication,
    QMdiSubWindow,
    QWidget,
    QFileDialog,
    QMessageBox,
)
from PyQt5 import QtGui
import os

from Ui_loadLogWindow import Ui_LogWindow
import time


class Worker(QThread):
    """
    Worker thread
    """

    progress = pyqtSignal(int)

    def run(self):
        """
        Your code goes in this function
        """
        print("Thread start")
        i = 1
        while True:
            time.sleep(0.5)
            self.progress.emit(i)
            i += 1


class LoadLogWidget(QWidget):
    def __init__(self, parent=None) -> None:
        super().__init__(parent=parent)
        self.__ui = Ui_LogWindow()
        self.__ui.setupUi(self)

        self.thread = Worker()
        self.thread.progress.connect(self.update_progress)

    def update_progress(self, progress):
        print(progress)

    def OnSelectHostPath(self):
        if self.thread.isRunning():
            reply = QMessageBox.question(
                self,
                "停止提取log ?",
                "确定停止 ?",
                QMessageBox.Yes | QMessageBox.No,
                QMessageBox.No,
            )
            if reply == QMessageBox.Yes:
                self.thread.terminate()
                self.thread.wait()
        else:
            fileName, fileType = QFileDialog.getOpenFileName(
                self, "选取文件", os.getcwd(), "All Files(*);;Text Files(*.txt)"
            )
            self.__ui.textEdit_host_path.setText(fileName)
            self.thread.start()

    def OnSelectDevicePath(self):
        fileName, fileType = QFileDialog.getOpenFileName(
            self, "选取文件", os.getcwd(), "All Files(*);;Text Files(*.txt)"
        )
        self.__ui.textEdit_device_path.setText(fileName)


class LoadLogSubWindow(QMdiSubWindow):
    close_signal = pyqtSignal(str)

    def __init__(self, parent=None) -> None:
        super().__init__(parent=parent)
        self.setWidget(LoadLogWidget())
        # self.setWindowTitle("设置窗口控件的风格")

    def closeEvent(self, a0: QtGui.QCloseEvent) -> None:
        self.close_signal.emit("haha")
        return a0.ignore()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    m = LoadLogWidget()
    m.show()
    sys.exit(app.exec_())